<template>
  <header class="header">
    <h1>todos</h1>
    <input id="toggle-all" class="toggle-all" type="checkbox" v-model="isAll"/>
    <label for="toggle-all"></label>

    <!-- 回车添加任务 -->
    <input
      v-model.trim="content"
      @keyup.enter="add"
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
    />
  </header>
</template>

<script>
export default {
  data() {
    return {
      // 接收添加任务的名
      content: "",
    };
  },
  methods: {
    add() {
      // 非空判断
      if (this.content.length === 0) {
        return;
      }
      this.$emit("add", this.content);
      this.content = "";
    },
  },
  computed: {
    // 全选功能
    isAll: {
      set(checked) {
        // 影响数组里每个小选框的选中状态
        this.arr.forEach((item) => (item.isDone = checked));
      },
      get() {
        return (
          // 小选框选中状态影响全选选中状态
          this.arr.length !== 0 && this.arr.every((item) => item.isDone)
        );
      },
    },
  },
  props: {
    arr: {
      type: Array,
      required: true,
    },
  },
};
</script>
